﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPMVC_first_app.Models;
using System.Data.Entity;


namespace ASPMVC_first_app.Controllers
{
    public class UserController : Controller
    {
        private ApplicationDbContext _context;

        public UserController()
        {
            _context = new ApplicationDbContext();
        }

        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
        }
        

        public ActionResult Index()
        {
            if(User.IsInRole("SuperAdmin"))
            {
                return View("CustomerList");
            }
            else
            {
                return View("ReadOnlyCustomersList");
            }
        }

        public ActionResult UserInfo(int id)
        {
            //var customer = _context.Customers.ToList();

            var customer = _context.Customers.Include(t => t.MembershipType).SingleOrDefault(c => c.Id == id);

            if (customer == null)
                return HttpNotFound();

            return View(customer);
        }

        [Authorize(Roles =UserRole.SuperAdmin)]
        public ActionResult New()
        {
            var membershiptypes = _context.MembershipTypes.ToList();
            var viewModel = new ViewModels.CustomerFormViewModel()
            {
                Customer = new Customer(),
                MembershipTypes = membershiptypes
            };
            return View("CustomerForm",viewModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Save(ViewModels.CustomerFormViewModel viewModel)
        {
            if(!ModelState.IsValid)
            {
                var viewModell = new ViewModels.CustomerFormViewModel()
                {
                    Customer = viewModel.Customer,
                    MembershipTypes = _context.MembershipTypes.ToList()
                };
                return View("CustomerForm", viewModell);
            }
            

            if (viewModel.Customer.Id==0)
            {
                _context.Customers.Add(viewModel.Customer);
            }
            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == viewModel.Customer.Id);

                //Mapper.Map(customer, customerInDb);

                customerInDb.FirstName = viewModel.Customer.FirstName;
                customerInDb.LastName = viewModel.Customer.LastName;
                customerInDb.DateOfBirth = viewModel.Customer.DateOfBirth;
                customerInDb.MembershipTypeId = viewModel.Customer.MembershipTypeId;
                customerInDb.IsSubscribed = viewModel.Customer.IsSubscribed;
            }
            
            _context.SaveChanges();
            return RedirectToAction("Index", "User");
        }

        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id ==id);

            if (customer==null)            
                return HttpNotFound();

            var viewModel = new ViewModels.CustomerFormViewModel()
            {
                Customer = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            return View("CustomerForm", viewModel);
        }

     
    }
}